<?php

/**
 * 
 * @author yuxun
 * 激活码使用接口
 */
class CodeController extends Controller {
	
	public $actionId;
	public function __construct($id,$module=null){
		parent::__construct();
	}
	
	public function beforeAction($action){
		$local_sign = $this->getSign();
	
		$this->actionId  = $action->getId();
	
		// 不参与签名验证的方法  //array('getLastNotice')
		if(!in_array($this->actionId, array())){
// 			ApiBaseService::doLog($_POST['sign'], array('sign'=>$local_sign));
// 			echo $local_sign;echo '<br/>';
// 			echo $_POST['sign'];echo '<br/>';exit;
			if($local_sign != $_POST['sign']){
				$json_arr = array('errcode'=>-1,'msg'=>'check_sign_err','data'=>'' );
				ApiBaseService::doLog($_POST, $json_arr);
				exit(json_encode($json_arr));
			}

		}
	
	
		return parent::beforeAction($action);
	}
	
	
	
	
	/**
	 * 请求签名
	 * 根据参数名称将所有请求参数按照字母先后顺序排序:key + value .... key + value 对除签名的所有请求参数按key做的升序排列, value为URL Encode之后的值。
	 * 例如：将foo=1,bar=2,baz=3 排序为bar=2,baz=3,foo=1 参数名和参数值链接后，得到拼装字符串bar2baz3foo1
	 * 再将字符串 拼接当前时间在拼接双方约定的key，拼接完后md5加密，再转化成大写，即 strtoupper(md5(bar2baz3foo1.time().key));
	 * @return type
	 */
	protected function getSign(){
		header("Content-type:text/html;charset=utf-8");
		$key = Yii::app()->params['API_SECRET_KEY'];
		$data = $_POST;
		
		unset($data['sign']);    // 删除这两个参数再来进行排序
		ksort($data);
		foreach($data as $k=>$v){
			$str .= '&'.$k.'='.$v;
		}
		$str = substr($str,1);
		//return $key.$str;
		return md5($key.$str);
		
	}

    public function check(){  	  	
    	$channel_id = PubRequest::R('channel_id');
    	$account = PubRequest::R('account');
    	$role_id = PubRequest::R('role_id');
    	$grade = PubRequest::R('grade');
    	$code = PubRequest::R('code');
    	
    	$json_arr = CodeService::check($channel_id,$account,$role_id,$grade,$code);
    	ApiBaseService::doLog($_POST, $json_arr);
    	exit(json_encode($json_arr));
    	
    }
    
    
   

}